#include "ui.h"
#include <fstream>
#include "dv_sdlgrafix.h"
using namespace std;

/*--- Constructors ---*/
ui::ui(SDL_Surface * a_screen, const char * a_image, int a_number, int a_x, int a_y, int a_w, int a_h)
: m_buffer(a_screen), m_int(false), m_struct(false), m_sPosition(true),
  m_width(a_w), m_hieght(a_h), m_tiles(a_number)
{
	m_ui = load_image(a_image);

	int temp = 0;
	m_position = new uiPosition[a_number];
	for(int i = 0; i < 2; i++){
		if(i == 0){
			for(int j = 0; j < 9; j++, temp++){
				m_position[temp].x = (j * m_width) + a_x;
				m_position[temp].y = a_y;
			}
		}
		if(i == 1){
			for(int j = 0; j < 6; j++, temp++){
				m_position[temp].x = (j * m_width) + a_x;
				m_position[temp].y = 766;
			}
		}
	}
}

ui::ui(SDL_Surface * a_screen, const char * a_file, const char * a_image, int a_number, int a_x, int a_y)
: m_buffer(a_screen), m_int(true), m_struct(false), m_sPosition(true),
  m_width(0), m_hieght(0), m_tiles(a_number)
{
	m_ui = load_image(a_image);
	m_iInterface = new int[a_number];
	m_position = new uiPosition[a_number];
	m_width = m_ui->h;

	for(int i = 0; i < a_number; i++){
		m_iInterface[i] = 0;

		m_position[i].x = (i * 112) + a_x;
		m_position[i].y = a_y;
	}

	initialize(a_file);
}

ui::~ui()
{
	if(m_int) delete [] m_iInterface;
	if(m_struct) delete [] m_interface;
	if(m_sPosition) delete [] m_position;
}

/*--- Initializers ---*/
void ui::initialize(const char * a_file)
{
	ifstream fin;
	int tX = 0; // temp variables for x and width;
	//fin.open("titleDim.txt");
	fin.open(a_file);

	if(!fin.is_open()) { exit(1); }

	for(int i = 0; i < 14; i++){
		if(fin.good()){
			// the first thing in the file is x, followed by width
			// store the variables in the interface
			fin >> m_iInterface[i];
		}
		fin.ignore(512, '\n'); // go to next line
	}
	fin.close();
}
/*--- Game Functions ---*/
void ui::drawCon1()
{
	SDL_Rect rect = {0, 0, m_width, m_width};

	for(int i = 0; i < m_tiles; i++){
		//rect.x = i * m_width;
		drawTile(m_ui, &rect, i, m_buffer, m_position[i].x, m_position[i].y);
	}
}

void ui::drawCon2()
{
	SDL_Rect rect = {0, 0, 0, m_hieght};
	
	for(int i = 0; i < m_tiles; i++){
		rect.w = m_iInterface[i];
		//drawTile(m_ui, &rect, m_buffer, m_position[i].x, m_position[i].y);
	}
}

void ui::release()
{
	SDL_FreeSurface(m_buffer);
	SDL_FreeSurface(m_ui);
}